<template>
  <div>
    <el-dialog width="30%" :title="dialogTitle" :visible.sync="dialogFormVisible1" @close="closeDialog" :destroy-on-close="true" :append-to-body="true" :close-on-click-modal="false">
      <el-form :model="userForm" :rules="userRules" ref="userForm">
        <el-form-item label="账号:" :label-width="formLabelWidth" prop="userName" v-show="false">
          <el-input v-model="userForm.userName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱:" :label-width="formLabelWidth" prop="email">
          <el-input v-model="userForm.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话:" :label-width="formLabelWidth" prop="phoneNumber">
          <el-input v-model="userForm.phoneNumber" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="昵称:" :label-width="formLabelWidth" prop="nickName">
          <el-input v-model="userForm.nickName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="性别:" :label-width="formLabelWidth" prop="sex">
          <el-radio-group v-model="userForm.sex">
            <el-radio label="0">男</el-radio>
            <el-radio label="1">女</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span style="display: inline-block;width: 60px; text-align: right">头像：</span>
      <el-upload
          class="avatar-uploader"
          :action="uploadUrl"
          :headers="uploadHeaders"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
        <img v-if="userForm.avatar" :src="userForm.avatar" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="addOrEditUser">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addUser, editUser} from "@/api/user";
import {Message} from "element-ui";

export default {
  name: "UserDialog",
  data() {
    return {
      uploadUrl: "http://localhost:9898/file/upload/no/expire",
      uploadHeaders: {
        'Authentication': sessionStorage.getItem("token")
      },
      formLabelWidth: '60px',
      userRules: {
        userName: [
          { required: true, message: '请输入账号名', trigger: 'blur' }
        ],
        phoneNumber: [
          { required: true, message: '请输入电话', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
      }
    }
  },
  computed: {
    dialogFormVisible1: {
      get() {
        return this.$store.state.user.dialogFormVisible1;
      },
      set(value) {
        this.$store.state.user.dialogFormVisible1 = value;
      }
    },
    dialogTitle() {
      return this.$store.state.user.dialogTitle;
    },
    userForm: {
      get() {
        return this.$store.state.user.userForm;
      },
      set(value) {
        this.$store.state.user.userForm = value;
      }
    }
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.userForm.avatar = file.response.data.url;
    },
    beforeAvatarUpload(file) {
      const isLt3M = file.size / 1024 / 1024 < 3;

      if (!isLt3M) {
        this.$message.error('上传头像图片大小不能超过 3MB!');
      }
      return isLt3M;
    },
    clearForm() {
      Object.keys(this.userForm).forEach((key) => {
        this.userForm[key] = null;
      })
    },
    closeDialog() {
      this.$store.state.user.dialogFormVisible1 = false;
      this.clearForm();
    },
    addOrEditUser() {
      this.$refs["userForm"].validate((valid) => {
        if (valid) {
          if (this.dialogTitle === "添加用户") {
            addUser(this.userForm).then((res) => {
              Message.success(res.data.msg);
              this.userForm = {};
              this.dialogFormVisible1 = false;
              Message.success(res.data.msg);
            })
          } else {
            editUser(this.userForm).then((res) => {
              Message.success(res.data.msg);
              this.dialogFormVisible1 = false;
              Message.success(res.data.msg);
              this.clearForm();
              sessionStorage.setItem("userInfo", JSON.stringify(res.data.data));
              location.reload();
            })
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  }
}
</script>

<style scoped>
.avatar-uploader {
  text-align: center;
  width: 100%
}
.avatar-uploader-icon:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 170px;
  height: 170px;
  line-height: 170px;
  text-align: center;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;

}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
